var app = getApp();
import { api, util, dialog} from '../../../../js/util'
import {
  router
} from '../../../../js/router';
import { fastorder, grouporder } from "../../../../js/modules/order.js"
import authorize from '../../../../js/modules/authorize.js'
import {
  wholesaleSettings
} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"myGroupList",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    mainColor:api.titleColor||"#e56b14",//主题色
    tablist: ['全部', '待付定金', '已付定金', '拼团成功', '拼团失败', '已作废'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 80,
    showMerge: true,
    cancelItems: [{ value: '现在不想购买', checked: 'checked' },
    { value: '支付不成功' },
    { value: '商品价格较高' },
    { value: '等待时间过长' },
    { value: '卖家缺货' },
    { value: '更换或者添加新商品' },
    { value: '错误或重复下单' },
    { value: '其他' }
    ],
    lists: [{
      status: 0,//全部
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      status: 1,//待付款
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    },
    {
      status: 2,//待发货
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    },{
      status: 3,//待收货
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      status: 4,//已完成
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      status: 5,//已作废
      list: [],
      hasMoreData: true,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }
    ],
    orderId: 0,
    flag:1,
    flag_des:"拼团",
    quantityDigits: 2,
    priceDigits: 2,//价格小数位数
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //1为拼团，2为预售
    let flag = options.flag || 1;
    this.data.flag = flag;
    this.data.flag_des = flag==1?"拼团":"预售";
    wx.setNavigationBarTitle({
      title: flag == 1 ? "我的拼团" : "我的预售"
    });
    this.pageKey = flag== 1 ? 'myGroupList' : 'myPresellList';
    //当为预售时，显示
    if(flag==2){
      this.data.tablist=['全部', '待付款', '交易成功', '已作废'];
      for(var i=this.data.lists.length-1;i>0;i--){
        var status = this.data.lists[i].status;
        if (!(status==0||status==1||status==3||status==5)){
          this.data.lists.splice(i,1);
        }
      }
      this.setData({
        flag: flag,
        flag_des: this.data.flag_des,
        tablist: this.data.tablist,
        lists:this.data.lists,
        slidewidth:70
      })
    }
    this.loadList();
    this.getWholesaleConfig();
    // this.cancelOrder = this.selectComponent("#cancelOrder");
  },
  /**加载商品列表 */
  loadList() {
    var that = this;
    var current = this.data.lists[that.data.activeIndex];
    var param = current.page;
    grouporder.loadList(current.status,that.data.flag, param, function (content, fkid) {
      var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
      var list_key = "lists[" + that.data.activeIndex + "].list";
      //是否有更多数据
      var list_has = "lists[" + that.data.activeIndex + "].hasMoreData";
      //无数据时显示的信息
      var isLoadComplete = "lists[" + that.data.activeIndex + "].isLoadComplete";
      var index = that.data.lists[that.data.activeIndex].list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (current.page.PageIndex > 1) {
        data = that.data.lists[that.data.activeIndex].list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        [list_key]: data,
        [list_has]: hasMoreData,
        [isLoadComplete]:true
      });

    })
  },
  /**加载配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var config = wholesaleSettings.getConfig();
    that.setData({
      quantityDigits: config.quantityDigits,
      priceDigits: config.priceDigits
    });
  },
  /**
   * 关键词搜索
   */
  onSearchKeywordEvent: function (e) {
    this.cleanList();
    for (var i = 0; i < this.data.lists.length; i++) {
      this.data.lists[i].page.keywords = e.detail;
    }
    this.loadList();
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.lists[this.data.activeIndex].page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var current = this.data.lists[this.data.activeIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeIndex].page.PageIndex;
      this.data.lists[this.data.activeIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Currency:api.Currency || '￥'
    })
    //清理数据
    // this.cleanList(this);
    // this.loadList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // console.log("hide");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 切换当前选择项
   */
  onSwitchItem: function (res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex
    });
    var current = this.data.lists[activeIndex];
    this.loadList();

  },

 
  /**
 * 取消拼团/预售
 */
  cancelOrder: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.orderid;
    let {flag_des} = that.data;
    util.confirm(function(){
      var data = {
        Id: id,
        Cause: "买家取消" + flag_des
      }
      grouporder.cancelOrder(data, () => {
        that.cleanList();
        that.loadList();
      });
    },null,'是否确认取消','取消'+flag_des);
  },
  // onCancelOrderEvent() {
  //   this.cleanList();
  //   this.loadList();
  // },

  /**
   * 立即付款
   */
  onPayEvent: function (e) {
    var id = e.currentTarget.dataset.id;
    var t = e.currentTarget.dataset.type;
    grouporder.onPay(id,t,(bo)=>{
      if(bo){
        this.cleanList();
        this.loadList();
      }
    });
  },
  
  /**
   * 清除每一列
   */
  cleanList: function () {
    let that=this;
    for (var i = 0; i < that.data.lists.length; i++) {
      that.data.lists[i].list = [];
      that.data.lists[i].page.PageIndex = 1;
    }
    that.setData({
      lists: that.data.lists
    });
  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  },
})